<?php
namespace app\common\lib;
use think\facade\Log;
class PingHelper
{
    public string $_err;


    function ping($host, $timeout = 1): bool
    {
        try {
            $output = shell_exec("ping -c 1 -W {$timeout} {$host}");
            //Log::info($output);
            if (strpos($output, "1 packets transmitted, 1 received") != false) {
                return true; // 主机在线
            }
        } catch (\Exception $e) {
            throw $e;
            $this->setErr($e->getMessage());
        }
        return false;
    }

    /**
     * @return string
     */
    public function getErr(): string
    {
        return $this->_err;
    }

    /**
     * @param string $err
     */
    public function setErr(string $err): void
    {
        $this->_err = $err;
    }

}
